$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/dict/list',
        datatype: "json",
        colModel: [
            {label: '代码项名称', name: 'codename', index: 'codename', width: 80},
            {label: '代码说明', name: 'remark', index: 'remark', width: 80},
            {label: '代码键', name: 'codeguid', index: 'codeguid', hidden: true},
            {
                label: '子项管理', index: '', width: 80, formatter: function (cellValue, options, rowObject) {
                    return '<a onclick="vm.addItem(\'' + rowObject.codeguid + '\')"><i  class="glyphicon glyphicon-list glyphicon-lg" /></a>'
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            codename: null,
            remark: null
        },
        dict: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {

            if (!vm.q.codename) {
                alert("代码项名称必须填写！");
                return;
            }

            $.ajax({
                type: "POST",
                url: baseURL + "sys/dict/save",
                contentType: "application/json",
                data: JSON.stringify(vm.q),
                success: function (r) {
                    if (r.code == 0) {
                        alert('操作成功', function (index) {
                            $("#jqGrid").trigger("reloadGrid");
                            vm.q.codename = null;
                            vm.q.remark = null;
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        addItem: function (codeGuid) {
            layer.open({
                type: 2,
                offset: '20px',
                skin: 'layui-layer-molv',
                title: "添加代码子项",
                area: ['920px', '450px'],
                shade: 0.3,
                shadeClose: false,
                content: "dictitem.html?codeGuid="+codeGuid

            });
        },
        del: function (event) {
            var rowDatas = getSelectedRows();
            if (rowDatas == null) {
                return;
            }

            var ids = [];
            rowDatas.forEach(function (rowData) {
                ids.push(rowData.codeguid);
            })

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/dict/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        reload: function (event) {
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'codeName': vm.q.codename},
                page: page
            }).trigger("reloadGrid");
        }
    }
});